from django.shortcuts import render
from rest_framework.views import APIView
# Create your views here.
from .models import User
from rest_framework.response import Response
from rest_framework.generics import CreateAPIView
from .serializers import CreateUserSerializer

# GET usernames/(?P<username>\w{5,20})/count/
class UsernameCountView(APIView):
    '''用户名数量'''
    def get(self, request, username):
        '''指定用户数量'''
        count = User.objects.filter(username=username).count()
        data = {
            'username': username,
            'count': count
        }
        return Response(data)

 # GET mobiles/(?P<mobile>1[3-9]\d{9})/count/
class MobileCountView(APIView):
    '''指定手机号是否以注册'''
    def get(self, request, mobile):
        '''指定手机号数量'''
        count = User.objects.filter(mobile=mobile).count()
        data = {
            'mobile': mobile,
            'count': count
        }
        return Response(data)

class UserView(CreateAPIView):
    """
       用户注册
       传入参数：
           username, password, password2, sms_code, mobile, allow
       """
    serializer_class = CreateUserSerializer


# POST /authorizations/ 登录
